package com.youlu.campus.admin.ca.controller;

import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.CaGrantQuery;
import com.youlu.campus.entity.VO.req.CreateCaGrantReq;
import com.youlu.campus.service.ca.impl.CaGrantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class CaGrantController {

    @Autowired
    private CaGrantService caGrantService;


    @PostMapping("/ca-grant/add-edit")
    public RestResult saveOrUpdate(@RequestBody @Validated CreateCaGrantReq createCaGrantReq,
                                   BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResultBuilder.builder().failure(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        caGrantService.saveOrUpdate(createCaGrantReq);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("/ca-grant/grant")
    public RestResult caGrant(@RequestParam("id") String grantId) {
        caGrantService.caGrant(grantId);
        return RestResultBuilder.builder().success().build();
    }

    @PostMapping("/ca-grant/list")
    public RestResult list(@RequestBody CaGrantQuery caGrantQuery) {
        return RestResultBuilder.builder().data(caGrantService.list(caGrantQuery)).success().build();
    }





}
